/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snAnalysis;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author FUJISAI
 */
public class GetBlogContent {

    private ArrayList blogs;

    public GetBlogContent() {
        this.blogs = new ArrayList<String>();

        MySQLBridge ms = new MySQLBridge();
        ResultSet rel = ms.getSingleField("blog_list", "blogID", "");
        try {
            while (rel.next()) {
                this.blogs.add(rel.getString("blogID"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int getTotalEntry(String blogID) {
        int total = 0;

        try {
            String url = "http://360.yahoo.com/blog-" + blogID + "?cq=0&cs=0&list=1";
            URL u = new URL(url);
            BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));
            String line = "";

            while ((line = in.readLine()) != null) {
                int start = line.indexOf("<em class=\"limit\">");
                int end = line.indexOf("</em>", start);
                if (start > 0) {
                    String temp = line.substring(start + 18, end);
                    total = Integer.parseInt(temp);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return total;
    }

    public static void main(String args[]) {
        GetBlogContent bc = new GetBlogContent();

        for (int i = 0; i < bc.blogs.size(); i++) {
            System.out.print((i + 1) + ". ");
            System.out.print(bc.getTotalEntry(bc.blogs.get(i).toString()));
            System.out.print("\n");
        }
    }
}
